package com.yancy.controller;

import com.mysql.cj.Session;
import com.yancy.entity.User;
import com.yancy.service.UserService;
import com.yancy.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        String userName = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User(userName,password);
        if(userService.userLogin(user)){
            req.getSession().setAttribute("user",user.getUserName());
            resp.sendRedirect("amdinLogin");
//            验证成功 进入后台管理界面
//            req.getRequestDispatcher("/WEB-INF/views/admin.jsp")
//                    .forward(req, resp);
        } else {
            req.setAttribute("res","error");//登录失败
            req.getRequestDispatcher("login.jsp")
                    .forward(req, resp);
        }
    }
}
